﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CashierSystem.CustomerManager;
using CashierSystem.UserManager;

namespace CashierSystem.CashierInManager
{
    public partial class CashierInForm : Form
    {
        string regNo;
        User user;
        public CashierInForm(String regNo, User user)
        {
            this.regNo = regNo;
            this.user = user;
            InitializeComponent();
        }

        string[] departId;
        string[] departName;

        private void CashierInForm_Load(object sender, EventArgs e)
        {
            label10.Text = CashierInManager.loadOneCustomerName(regNo);

            CashierInManager.getDepartInfo(out departName, out departId);
            cbDepartment.Items.Clear();
            for (int i = 0; i < departId.Length; i++)
            {
                cbDepartment.Items.Add(departName[i]);
            }
        }

        string[] itemId;
        string[] itemName;
        string[] price;
        string[] doctorId;
        string[] doctorName;
        string[] tiaoliDoctorId;
        string[] tiaoliDoctorName;
        string[] expertDoctorId;
        string[] expertDoctorName;


        private void cbCheckItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            int i = cbCheckItems.SelectedIndex;
            string price1 = price[i].ToString();
            tbPrice.Text = price1;
            string itemId1 = itemId[i].ToString();
            CashierInManager.getDoctorInfo(out doctorName, out doctorId, itemId1);
            CashierInManager.getTiaoLiDoctorInfo(out tiaoliDoctorName,out tiaoliDoctorId,itemId1);
            CashierInManager.getExpertDoctorInfo(out expertDoctorName,out expertDoctorId,itemId1);
            cbDoctor.Items.Clear();
            cbDoctor1.Items.Clear();
            cbDoctor2.Items.Clear();
            cbDoctor3.Items.Clear();
            cbDoctor4.Items.Clear();
            cbDoctor5.Items.Clear();
            for (int j = 0; j < doctorId.Length; j++)
            {
                cbDoctor.Items.Add(doctorName[j]);
            }
            cbDoctor1.Items.Clear();
            for (int j = 0; j < doctorId.Length; j++)
            {
                cbDoctor1.Items.Add(doctorName[j]);
            }
            cbDoctor2.Items.Clear();
            for (int j = 0; j < tiaoliDoctorId.Length; j++)
            {
                cbDoctor2.Items.Add(tiaoliDoctorName[j]);
            }
            cbDoctor3.Items.Clear();
            for (int j = 0; j < tiaoliDoctorId.Length; j++)
            {
                cbDoctor3.Items.Add(tiaoliDoctorName[j]);
            }
            cbDoctor4.Items.Clear();
            for (int j = 0; j < expertDoctorId.Length; j++)
            {
                cbDoctor4.Items.Add(expertDoctorName[j]);
            }
            cbDoctor5.Items.Clear();
            for (int j = 0; j < expertDoctorId.Length; j++)
            {
                cbDoctor5.Items.Add(expertDoctorName[j]);
            }

        }

        /*private void cbDiscount_SelectedIndexChanged(object sender, EventArgs e)
        {
            double discount = 0 ;
            if(cbDiscount.Text == "全价")
            {
                discount = 10;
            }
                else if(cbDiscount.Text == "免费")
                {
                    discount = 0;
                }
                    else
                    {
                        discount = double.Parse(cbDiscount.Text);

                    }
           // double discount = double.Parse(cbDiscount.Text);
            double price = double.Parse(tbPrice.Text);
            double actualPrice = price * discount / 10;
            tbActualPrice.Text = actualPrice.ToString();

        }*/

        double totalPrice = 0;
         
        private void button2_Click(object sender, EventArgs e)
        {
            string customerId = regNo;
            int i = cbCheckItems.SelectedIndex;
            string itemId1 = itemId[i].ToString();
            int j = cbDoctor.SelectedIndex;
            int j1 = cbDoctor1.SelectedIndex;
            int j2 = cbDoctor2.SelectedIndex;
            int j3 = cbDoctor3.SelectedIndex;
            int j4 = cbDoctor4.SelectedIndex;
            int j5 = cbDoctor5.SelectedIndex;
            string doctorId1 = "";
            if (j != -1 && j != 0)
            {
                doctorId1 = doctorName[j];
            }
            else
            {
                doctorId1 = "";
            }
            j = cbDoctor1.SelectedIndex;
            string doctorId2 = "";
            if (j1 != -1 && j1!= 0)
            {
                doctorId2 = doctorName[j];
            }
            else
            {
                doctorId2 = "";
            }
            j = cbDoctor2.SelectedIndex;
            string doctorId3 = "";
            if (j2 != -1 && j2 != 0)
            {
                doctorId3 = tiaoliDoctorName[j];
            }
            else
            {
                doctorId3 = "";
            }
            j = cbDoctor3.SelectedIndex;
            string doctorId4 = "";
            if (j3 != -1 && j3 != 0)
            {
                doctorId4 = tiaoliDoctorName[j];
            }
            else
            {
                doctorId4 = "";
            }
            j = cbDoctor4.SelectedIndex;
            string doctorId5 = "";
            if (j4 != -1 && j4 != 0)
            {
                doctorId5 = expertDoctorName[j];
            }
            else
            {
                doctorId5 = "";
            }
            j = cbDoctor5.SelectedIndex;
            string doctorId6 = "";
            if (j5 != -1 && j5 != 0)
            {
                doctorId6 = expertDoctorName[j];
            }
            else
            {
                doctorId6 = "";
            }
            //string doctorId = "";

            string expertFee = tbExpertFee.Text;
            if (expertFee == "")
            {
                expertFee = "0";
            }

            CashierInManager.saveCheckItems(customerId, itemId1,doctorId1, doctorId2, doctorId3, doctorId4, doctorId5, doctorId6, expertFee);


            string sensingId = CashierInManager.getMaxSensingId();
            string cashierId = user.getAccount();
            if (cbPayMethod.Text != "")
            {
                string payMethod = cbPayMethod.Text;
                string payMoney = tbPayMoney.Text;
                string discount = tbDiscount.Text;
                string cardId = "";
                CashierInManager.savePay(payMethod, payMoney, discount, sensingId, cashierId,cardId);
            }
            if (cbPayMethod1.Text != "")
            {
                string payMethod = cbPayMethod1.Text;
                string payMoney = tbPayMoney1.Text;
                string discount = tbDiscount1.Text;
                string cardId = "";
                CashierInManager.savePay(payMethod, payMoney, discount, sensingId, cashierId,cardId);
            }
            if (tbCardId.Text != "")
            {
                string payMethod = "消费卡";
                string payMoney = tbDeduct.Text;
                string discount = tbDiscount2.Text;
                string cardId = tbCardId.Text;
                CashierInManager.savePay(payMethod, payMoney, discount, sensingId, cashierId,cardId);
            }
            ListViewItem item = new ListViewItem();
            item.SubItems.Clear();
            item.SubItems[0].Text = cbCheckItems.Text;
            //item.SubItems.Add(cbDoctor.Text);
            item.SubItems.Add(tbPrice.Text);
            item.SubItems.Add(sensingId);
            item.SubItems.Add(expertFee);
            listView1.Items.Add(item);


            if (tbExpertFee.Text != "")
            {
                totalPrice += double.Parse(tbExpertFee.Text);
            }
            tbTotalPrice.Text = totalPrice.ToString();
            DialogResult response = MessageBox.Show("添加成功！");
            cbDepartment.Text = "";
            cbCheckItems.Text = "";
            tbPrice.Text = "";
            tbExpertFee.Text = "";
            cbDoctor.SelectedIndex = 0;
            cbDoctor1.SelectedIndex = 0;
            cbDoctor2.SelectedIndex = 0;
            cbDoctor3.SelectedIndex = 0;
            cbDoctor4.SelectedIndex = 0;
            cbDoctor5.SelectedIndex = 0;
           /* CustomerManagerAction cma = new CustomerManagerAction(customer);
            cma.save();
            ListViewItem item = new ListViewItem();
            item.SubItems.Clear();
            item.SubItems[0].Text = customerId;
            item.SubItems.Add(cutomerName);
            item.SubItems.Add(workDept);
            item.SubItems.Add(tel);

            listView1.Items.Add(item);
            DialogResult response = MessageBox.Show("添加成功！");*/
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count != 1)
            {
                DialogResult response = MessageBox.Show("请选择一条记录！");
                return;

            }
            else
            {
                DialogResult response = MessageBox.Show("您确定要删除这个项目吗？", "请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                if (response == System.Windows.Forms.DialogResult.Yes)
                {
                    string sensingId = listView1.SelectedItems[0].SubItems[4].Text;
                    CashierInManager.deleteSensingItem(sensingId);
                    double price1 = double.Parse(listView1.SelectedItems[0].SubItems[3].Text);
                    double price2 = double.Parse(listView1.SelectedItems[0].SubItems[5].Text);
                    totalPrice = double.Parse(tbTotalPrice.Text);
                    //double totalPrice1 = double.Parse(totalPrice);
                    double totalPrice2 = totalPrice - price1 - price2;

                    tbTotalPrice.Text = totalPrice2.ToString();
                    totalPrice = double.Parse(tbTotalPrice.Text);
                    listView1.Items.Remove(listView1.SelectedItems[0]);
                    response = MessageBox.Show("已删除");

                }
                    
            }
            
        }

        private void cbDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            int j = cbDepartment.SelectedIndex;
            string departId1 = departId[j];

            CashierInManager.getItemInfo(out itemName, out itemId, out price, departId1);

            cbCheckItems.Items.Clear();

            for (int i = 0; i < itemId.Length; i++)
            {
                cbCheckItems.Items.Add(itemName[i]);
            }
        }


        private void button1_Click(object sender, EventArgs e)
        {
            DialogResult r = MessageBox.Show("你确定收费已完成?点击“是”退出收费窗口", "请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
            if (r == System.Windows.Forms.DialogResult.Yes)
            {
                this.Close();
            }
            else 
            {
                return;
            }
        }

       /* private void tbActualPrice_TextChanged(object sender, EventArgs e)
        {
            if (double.Parse(tbActualPrice.Text) > double.Parse(tbPrice.Text))
            {
                DialogResult response = MessageBox.Show("请输入有效数值！");
                tbDiscountMoney.Text = "";
                return;
            }
            else
            {
                double discountMoney = double.Parse(tbPrice.Text) - double.Parse(tbActualPrice.Text);
                tbDiscountMoney.Text = discountMoney.ToString();
            }
            
        }

        private void tbDiscountMoney_TextChanged(object sender, EventArgs e)
        {
            if (double.Parse(tbDiscountMoney.Text) > double.Parse(tbPrice.Text))
            {
                DialogResult response = MessageBox.Show("请输入有效数值！");
                tbActualPrice.Text = "";
                return;
            }
            else 
            {
                double actualPrice = double.Parse(tbPrice.Text) - double.Parse(tbDiscountMoney.Text);
                tbActualPrice.Text = actualPrice.ToString();
            }
        }
*/






    }
}